# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : Web.py
# Time       ：2023/12/17 17:03
# Author     ：Y-aong
# version    ：python 3.10
# Description：UI自动化
"""
from pydantic import Field
from robot.libraries.Remote import Remote

from conf.config import OrderLinesConfig, RemoteWebConfig
from orderlines.libraries.BaseTask import BaseTask
from orderlines.utils.base_orderlines_type import BasePluginParam


class WebConfigParam(BasePluginParam):
    execute_path: str = Field(description='执行路径', default=r'C:\Program Files\Google\Chrome\Application\chrome.exe')
    max_size: bool = Field(description='是否全屏', default=True)
    height: int = Field(description='浏览器高度', default=1920)
    width: int = Field(description='浏览器宽度', default=1080)
    max_wait_time: int = Field(description='超时时间', default=60)


class GoToPageParam(BasePluginParam):
    url: str = Field(description='目标url')


class InputParam(BasePluginParam):
    selector: str = Field(description='元素选择器')
    text: str = Field(description='输入值')
    timeout: int = Field(description='超时时间', default=30)


class ClickParam(BasePluginParam):
    selector: str = Field(description='元素选择器')
    validate_selector: str = Field(description='验证选择器', default=None)
    double: bool = Field(description='是否双击', default=True)
    offset_x: int = Field(description='x轴偏移距离', default=0)
    offset_y: int = Field(description='y轴偏移距离', default=0)
    timeout: int = Field(description='超时时间', default=30)


class GetValueParam(BasePluginParam):
    selector: str = Field(description='元素选择器')
    attribute: str = Field(description='元素属性', default='text')
    timeout: int = Field(description='超时时间', default=30)


class KeyboardParam(BasePluginParam):
    selector: str = Field(description='元素选择器')
    keyboard: str = Field(description='键盘值')
    timeout: int = Field(description='超时时间', default=30)


class WaitElementParam(BasePluginParam):
    selector: str = Field(description='元素选择器')
    timeout: str = Field(description='等待时间', default=30)


class ExecuteJSParam(BasePluginParam):
    js_code: str = Field(description='js代码')


class ClosePageParam(BasePluginParam):
    page_number: int = Field(description='页面编码', default=1)


class ElementNumsParam(BasePluginParam):
    selector: str = Field(description='元素选择器')
    timeout: str = Field(description='等待时间', default=30)


class SaveScreenshotParam(BasePluginParam):
    image_path: str = Field(description='图片路径')


class ElementRectParam(BasePluginParam):
    selector: str = Field(description='元素选择器')
    timeout: str = Field(description='等待时间', default=30)


class DragAndDropParam(BasePluginParam):
    source_selector: str = Field(description='源元素选择器')
    target_selector: str = Field(description='目标元素选择器')
    timeout: str = Field(description='等待时间', default=30)


class UI(BaseTask):
    version = OrderLinesConfig.version
    title = 'WEB自动化'
    category = '自动化测试'
    icon = 'StarFilled'

    def __init__(self):
        super(UI, self).__init__()
        self.remote = Remote(RemoteWebConfig.uri)

    def _run_keyword(self, keyword_name: str, param=None):
        """获取关键的参数"""
        arguments = self.remote.get_keyword_arguments(keyword_name)
        keyword_arguments = dict()
        if param:
            for argument in arguments:
                if '=' in argument:
                    param_list = argument.split('=')
                    keyword_arguments[param_list[0]] = getattr(param, param_list[0])
                else:
                    keyword_arguments[argument] = getattr(param, argument)

        return self.remote.run_keyword(keyword_name, args=(), kwargs=keyword_arguments)

    def web_config(self, param: WebConfigParam):
        """浏览器配置"""
        self._run_keyword('web_config', param)
        return {'status': self.success}

    def goto_page(self, param: GoToPageParam):
        """打开页面"""
        self._run_keyword('goto_page', param)
        return {'status': self.success}

    def input(self, param: InputParam):
        """输入文字"""
        self._run_keyword('input', param)
        return {'status': self.success}

    def click(self, param: ClickParam):
        """鼠标点击"""
        self._run_keyword('click', param)
        return {'status': self.success}

    def get_element_attribute(self, param: GetValueParam):
        """标签取值"""
        element_attribute = self._run_keyword('get_element_attribute', param)
        return {'status': self.success, 'element_attribute': element_attribute}

    def wait_for_element(self, param: WaitElementParam):
        """等待元素"""
        self._run_keyword('wait_for_element', param)
        return {'status': self.success}

    def keyboard(self, param: KeyboardParam):
        """获取元素位置大小"""
        self._run_keyword('keyboard', param)
        return {'status': self.success}

    def get_element_rect(self, param: ElementRectParam):
        """获取元素位置大小"""
        self._run_keyword('get_element_rect', param)
        return {'status': self.success}

    def execute_js(self, param: ExecuteJSParam):
        """执行js"""
        response = self._run_keyword('execute_js', param)
        return {'status': self.success, 'response': response}

    def drag_and_drop(self, param: DragAndDropParam):
        """执行js"""
        response = self._run_keyword('drag_and_drop', param)
        return {'status': self.success, 'response': response}

    def close_page(self, param: ClosePageParam):
        """关闭页面"""
        self._run_keyword('close_page', param)
        return {'status': self.success}

    def save_screenshot(self, param: SaveScreenshotParam):
        """关闭页面"""
        self._run_keyword('save_screenshot', param)
        return {'status': self.success}

    def get_elements_num(self, param: ElementNumsParam):
        """关闭页面"""
        self._run_keyword('get_elements_num', param)
        return {'status': self.success}

    def go_forward(self):
        """页面前进"""
        self._run_keyword('go_forward')
        return {'status': self.success}

    def go_back(self):
        """页面后退"""
        self._run_keyword('go_back')
        return {'status': self.success}

    def refresh(self):
        """页面刷新"""
        self._run_keyword('refresh')
        return {'status': self.success}

    def quit(self):
        """关闭浏览器"""
        self._run_keyword('quit')
        return {'status': self.success}
